//
//  InformationViewController.m
//  FunSport_WWL
//
//  Created by 王黎 on 16/4/28.
//  Copyright © 2016年 wanghao. All rights reserved.
//

#import "InformationViewController.h"
//#import "NewsListModel.h"

@interface InformationViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property(nonatomic,strong)NSMutableArray *dataSource; //接受数据数组
@property(nonatomic,strong)NSMutableArray *scrArray;
@property(nonatomic,strong)SDCycleScrollView *sdCycleScrollView;

@end

@implementation InformationViewController


#pragma mark ------懒加载---------

- (NSMutableArray *)dataSource {
    
    if (!_dataSource) {
        
        _dataSource = [NSMutableArray array];
    }
    return  _dataSource;
}

- (NSMutableArray *)scrArray {
    
    if (!_scrArray) {
        
        _scrArray = [NSMutableArray array];
    }
    return _scrArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self createsdCycleScrollView];
    [self fetchDataWithUrl:kNBAListUrl];
    // Do any additional setup after loading the view from its nib.
    
    [self.tableView registerNib:[UINib nibWithNibName:@"InformationCell" bundle:nil] forCellReuseIdentifier:@"CELL"];
}

//创建轮播图
//-(void)createsdCycleScrollView{
//    NSLog(@"轮播图");
//    _sdCycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 64, kWidth, kHeight/3-64) imageURLStringsGroup:nil];
//
//    [self.view addSubview:_sdCycleScrollView];
//    
//}

#pragma mark --网络请求

- (void)fetchDataWithUrl:(NSString *)url {
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        
        [manager GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSLog(@"get nba success");
            
            NSDictionary *json_dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
            NSArray *nbaArray = json_dic[@"T1348649145984"];
            
            for (NSDictionary *dic in nbaArray) {
                
                NewsListModel *model = [[NewsListModel alloc]init];
                [model setValuesForKeysWithDictionary:dic];
                
                NSLog(@"%@",model.title);
                [self.dataSource addObject:model];
            }
            
            [self.tableView reloadData];
            
//            [self createsdCycleScrollView];
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"wrong");
        }];
        
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    
}



#pragma mark -- tableview方法
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    
    InformationCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL" forIndexPath:indexPath];
    
    [cell setCellWithModel:self.dataSource[indexPath.row]];
    
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return self.dataSource.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{

    return 150;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath{

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
